﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using System.ServiceModel;
using GalaSoft.MvvmLight;
using TriviaViewModel.ServiceReference1;



namespace TriviaViewModel
{
    /// <summary>
    /// the view model of entrancecontrol
    /// </summary>
    public class EntryViewModelWizard : ViewModelBase
    {
        int numberOfAboutClicks = 0;

        #region proerties

        /// <summary>
        /// binded to the text box of user name
        /// </summary>
        private string _usereNam;
        public string UserName
        {
            get { return _usereNam; }
            set
            {
                if (_usereNam != value)
                {
                    _usereNam = value;
                    RaisePropertyChanged("UserName");
                }

            }
        }
        /// <summary>
        /// binded to visibility property, whether the is about visible
        /// </summary>

        /// <summary>
        /// binded to the text box of password
        /// </summary>
        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    RaisePropertyChanged("Password");
                }

            }
        }

      

        /// <summary>
        /// binding the list of topics in the combo box
        /// </summary>
        private List<string> _topics;
        public List<string> Topics
        {
            get { return _topics; }
            set
            {
                if (_topics != value)
                {
                    _topics = value;
                    RaisePropertyChanged("Topics");
                }
            }
        }
        /// <summary>
        ///  property that binds to the selected type of the combobox of type of questions
        /// </summary>
        private string _selectedTopic;
        public string SelectedTopic
        {
            get { return _selectedTopic; }
            set
            {
                if (_selectedTopic != value)
                {
                    _selectedTopic = value;
                    RaisePropertyChanged("SelectedTopic");
                }
            }
        }
        /// <summary>
        ///  property that binds to the selected difficulty of the combobox of type of questions
        /// </summary>
        private string _selectedDifficulty;
        public string SelectedDifficulty
        {
            get { return _selectedDifficulty; }
            set
            {
                if (_selectedDifficulty != value)
                {
                    _selectedDifficulty = value;
                    RaisePropertyChanged("SelectedDifficulty");
                }
            }
        }

        /// <summary>
        /// binding the list of difficulties in the combo box
        /// </summary>
        private List<string> _difficulties;
        public List<string> Difficulties
        {
            get { return _difficulties; }
            set
            {
                if (_difficulties != value)
                {
                    _difficulties = value;
                    RaisePropertyChanged("Difficulties");
                }
            }

        }

        public ICommand EndGameCommand { get; set; }
        public ICommand GoToGameCommand { get; set; }




        #endregion

        public EntryViewModelWizard()
        {
            IsGameVisible = Visibility.Collapsed;
            
            using (var proxy = new GameServiceClient())
            {
                Topics = proxy.GetTopics().ToList();
                Difficulties = proxy.GetDificulties().ToList();
            }
            GoToGameCommand = new RelayCommand(() => ExecuteGoToGameCommand(),
                () =>
                {
                    return SelectedDifficulty != null && SelectedTopic != null;
                });

            EndGameCommand = new RelayCommand(() => EndGameCommandExecute());

        }

        private GameViewModelWizard _gameViewModel;
        public GameViewModelWizard GameViewModel
        {
            get { return _gameViewModel; }
            set
            {
                if (_gameViewModel != value)
                {
                    _gameViewModel = value;
                    RaisePropertyChanged("GameViewModel");
                }
            }
        }

        private void ExecuteGoToGameCommand()
        {
            IsPlayGameButtonEnabled = true;
            GameViewModel = new GameViewModelWizard();
            if (GameViewModel.SetQuestionsAndAnswers(SelectedDifficulty, SelectedTopic))
            {

                IsGameVisible = Visibility.Visible;
                IsEntryVisible = Visibility.Collapsed;
                IsButtonsVisible = Visibility.Visible;

                if (UserName != null && Password != null)
                    GameViewModel.SetUser(UserName, Password);

            }
            else
                MessageBox.Show("no answers and questions avalible");





        }

        private void EndGameCommandExecute()
        {
            GameViewModel.EndGameOutside();
            UserName = null;
            Password = null;
            SelectedDifficulty = null;
            SelectedTopic = null;
            IsGameVisible = Visibility.Collapsed;
            IsEntryVisible = Visibility.Visible;
            IsButtonsVisible = Visibility.Collapsed;
            GameViewModel.EndGameOutside();
        }


        private Visibility _isButtonsVisible;
        public Visibility IsButtonsVisible
        {
            get { return _isButtonsVisible; }
            set
            {
                if (_isButtonsVisible != value)
                {
                    _isButtonsVisible = value;
                    RaisePropertyChanged("IsButtonsVisible");
                }
            }
        }

        private Visibility _isEntryVisible;
        public Visibility IsEntryVisible
        {
            get { return _isEntryVisible; }
            set
            {
                if (_isEntryVisible != value)
                {
                    _isEntryVisible = value;
                    RaisePropertyChanged("IsEntryVisible");
                }
            }
        }

        private Visibility _isGameVisible;
        public Visibility IsGameVisible
        {
            get { return _isGameVisible; }
            set
            {
                if (_isGameVisible != value)
                {
                    _isGameVisible = value;
                    RaisePropertyChanged("IsGameVisible");
                }
            }
        }



    }
}
